package com.wlts.wzy.DYUserModule.util;

import com.alibaba.fastjson.JSONObject;
import com.wlts.wzy.api.response.dy.DyOauthResponse;
import com.wlts.wzy.util.http.HttpClient;
import lombok.SneakyThrows;

public class UserUtil {


    //商家请求授权
    @SneakyThrows
    public static DyOauthResponse getAccessToken(String appKey, String appSecret ){

        String url="https://openapi-fxg.jinritemai.com/oauth2/access_token?app_id="+appKey+"&app_secret="+appSecret+"&grant_type=authorization_self";

        JSONObject jsonObject =  HttpClient.doGet(url);

        if(jsonObject==null){
            return null;
        }
        if(jsonObject.getInteger("err_no")!=0){
            System.out.println(jsonObject);
            return null;
        }

        JSONObject data=jsonObject.getJSONObject("data");

        DyOauthResponse response=JSONObject.toJavaObject(data,DyOauthResponse.class);

     //   System.err.println(response.toString());

        return response;

    }

    //商家请求授权
    @SneakyThrows
    public static DyOauthResponse RefreshToken(String appKey,String appSecret,String  refreshToken){

        String url="https://openapi-fxg.jinritemai.com/oauth2/refresh_token?app_id="+appKey+"&app_secret="+appSecret+"&grant_type=refresh_token&refresh_token="+refreshToken;

        JSONObject jsonObject =  HttpClient.doGet(url);

        if(jsonObject==null){
            return null;
        }
        if(jsonObject.getInteger("err_no")!=0){
            System.out.println(jsonObject);
            return null;
        }

        JSONObject data=jsonObject.getJSONObject("data");

        DyOauthResponse response=JSONObject.toJavaObject(data,DyOauthResponse.class);

   //     System.err.println(response.toString());

        return response;

    }




    public static void main(String[] args) {
//        //授权
        String appKey = "6920503223405200909";
        String appSecret = "3ea9bcf2-3f1c-425c-b369-588bf11405a2";
                getAccessToken(appKey,appSecret);
       //更新授权

      //  String refreshToken = "dd05f162-6894-45d6-a33f-597ed2d40d43";

      //  RefreshToken(appKey,appSecret,refreshToken);


    }

}
